<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('供应商管理')"/>
	<style>
		[v-cloak] {
			display: none;
		}
		.ul_ {
			position: absolute;
			right: 0;
			width: 200px;
			height: 200px;
			z-index: 999999;
			background-color: #fff;
			overflow: auto;
			border: 1px solid #dddddd;
		}
		.ul_::-webkit-scrollbar {
			display: none;
		}
		.ul_ li {
			cursor: pointer;
			width: 100%;
			text-indent: 5px;
			margin: 0;
		}
		.ul_ li:hover {
			background: #c8c8c9;
		}
	</style>
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li>
								供应商编码：<input type="text" name="supplierCode"/>
							</li>
							<li style="position: relative">
								供应商名称：
								<input type="text" class="supplierInput" v-model="inputSupplierName" @focus="focusSupplier()" @blur="supplierInputYZ()"/>
								<ul id="ul_supplier" class="ul_" v-show="showSupplierUl">
									<li v-for="(item, index) in supplierList" :key="index" @click="chooseSupplier(item.supplierName, item.supplierId)" v-cloak>{{item.supplierName}}</li>
								</ul>
								<input type="hidden" name="supplierId" v-model="inputSupplierId">
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()" @click="supplierReset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
	        <div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:supplier:add">
					<i class="fa fa-plus"></i> 添加
				</a>
				<a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()" shiro:hasPermission="system:supplier:edit">
					<i class="fa fa-edit"></i> 修改
				</a>
				<a class="btn btn-danger btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:supplier:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>

    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:supplier:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:supplier:remove')}]];
        var prefix = ctx + "system/supplier";

		var app = new Vue({
			el: '.container-div',
			data: {
				suppliers: [],
				inputSupplierName: '',
				inputSupplierId: '',
				showSupplierUl: false,
				supplierList: []
			},
			methods: {
				//函数防抖
				supplier_debounce(wait){
					clearTimeout(this.timer);
					this.timer = setTimeout(() => {
						this.getSelectSupplierData(this.inputSupplierName.trim());
					}, wait)
				},
				//已选供应商
				chooseSupplier(val, id){
					this.inputSupplierName = val;
					this.inputSupplierId = id;
				},
				//重置输入供应商
				supplierReset(){
					this.inputSupplierName = '';
					this.inputSupplierId = '';
				},
				//验证输入供应商
				supplierInputYZ(){
					if(this.inputSupplierName.trim() != ''){
						var arr = [];
						for(var i = 0; i < this.suppliers.length; i++){
							arr.push(this.suppliers[i].supplierName);
						}
						var index = $.inArray(this.inputSupplierName.trim(), arr);
						if(index < 0){
							$.modal.alertError("不存在该供应商");
							this.supplierReset();
						}
					}else{
						this.supplierReset();
					}
				},
				//供应商输入框聚焦
				focusSupplier(){
					if(document.getElementsByClassName('supplierInput')[0] == document.activeElement){
						this.supplier_debounce(500);
					}
					this.showSupplierUl = true;
				},
				//根据供应商输入框内容匹配供应商列表
				getSelectSupplierData(val){
					this.supplierList = [];
					for(var i = 0; i < this.suppliers.length; i++){
						if(this.suppliers[i].supplierName.indexOf(val) != -1){
							this.supplierList.push(this.suppliers[i]);
						}
					}
				},
				//获取供应商列表
				getSuppliers: function(){
					var that = this;
					$.ajax({
						cache: true,
						type: "POST",
						url: "/system/supplier/list",
						data: {},
						async: false,
						error: function(request){
							$.modal.alertError("系统错误");
						},
						success: function(data){
							that.suppliers = data.rows;
						}
					});
				}
			},
			created(){
				this.getSuppliers();
			},
			watch: {
				'inputSupplierName': {
					handler: function(){
						this.focusSupplier();
					}
				}
			},
			mounted(){
				let that = this;
				document.addEventListener('click', function(e){
					if(e.target.className != 'supplierInput'){
						that.$nextTick(() => {
							that.showSupplierUl = false;
						})
					}
				})
				document.getElementById('ul_supplier').onmousedown = function(e){
					if(e && e.preventDefault){
						e.preventDefault();
					}else{
						window.event.returnValue = false;
					}
					return false;
				};
			}
		})

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                modalName: "供应商管理",
                columns: [
                	{
		        	    checkbox: true
		        	},
					{
						field: 'supplierId',
						title: '序号',
						align: 'center',
						visible: false
					},
					{
						field: 'supplierCode',
						title: '供应商编码',
						align: 'center'
					},
					{
						field: 'supplierName',
						title: '供应商名称',
						align: 'center'
					},
					{
						title: '操作',
						align: 'center',
						formatter: function(value, row, index){
							var actions = [];
							actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.supplierId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
							actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.supplierId + '\')"><i class="fa fa-remove"></i>删除</a>');
							return actions.join('');
						}
					}
				]
            };
            $.table.init(options);
        });
    </script>
</body>
</html>
